<!-- This HTML file has been created by texi2html 1.30
     from ../r4rs.texi on 14 July 1994 -->

<TITLE>Scheme - Expressions</TITLE>
<P>Go to the <A HREF="r4rs_5.htm">previous</A>, <A HREF="r4rs_7.htm">next</A> section.<P>
<H1><A NAME="SEC25" HREF="r4rs_toc.htm#SEC25">Expressions</A></H1>
<P>
A Scheme expression is a construct that returns a value, such as a
variable reference, literal, procedure call, or conditional.
<P>
Expression types are categorized as <EM>primitive</EM> or <EM>derived</EM>.
Primitive expression types include variables and procedure calls.
Derived expression types are not semantically primitive, but can instead
be explained in terms of the primitive constructs as in
section <A HREF="r4rs_9.htm#SEC78">derived expression types</A>.  They are redundant in the strict sense of
the word, but they capture common patterns of usage, and are therefore
provided as convenient abbreviations.
<P>
<H2><A NAME="SEC26" HREF="r4rs_toc.htm#SEC26">Primitive expression types</A></H2>
<P>
<H3><A NAME="SEC27" HREF="r4rs_toc.htm#SEC27">Variable references</A></H3>
<P>
<A NAME="IDX54"></A>
<U>essential syntax:</U> <B>&#60;variable&#62;</B><P>
<P>
An expression consisting of a variable
<A NAME="IDX55"></A>
<P>
(section <A HREF="r4rs_5.htm#SEC20">Variables and regions</A>) is a variable reference.  The value of
the variable reference is the value stored in the location to which the
variable is bound.  It is an error to reference an
unbound
<A NAME="IDX56"></A>
variable.
<P>
<PRE>
(define x 28)
x                           =>  28
</PRE>
<P>
<H3><A NAME="SEC28" HREF="r4rs_toc.htm#SEC28">Literal expressions</A></H3>
<P>
<A NAME="IDX57"></A>
<U>essential syntax:</U> <B>quote</B> <I>&#60;datum&#62;</I><P>
<A NAME="IDX58"></A>
<U>essential syntax:</U> <B>'&#60;datum&#62;</B><P>
<A NAME="IDX59"></A>
<U>essential syntax:</U> <B>&#60;constant&#62;</B><P>
<P>
<CODE>(quote &#60;datum&#62;)</CODE> evaluates to &#60;datum&#62;.
<A NAME="IDX60"></A>
&#60;Datum&#62; may be any external representation of a Scheme object (see
section <A HREF="r4rs_5.htm#SEC22">External representations</A>).  This notation is used to
include literal constants in Scheme code.
<A NAME="IDX61"></A>
<P>
<PRE>
(quote a)                   =>  a
(quote #(a b c))            =>  #(a b c)
(quote (+ 1 2))             =>  (+ 1 2)
</PRE>
<P>
<CODE>(quote &#60;datum&#62;)</CODE> may be abbreviated as
'&#60;datum&#62;.  The two notations are equivalent in all
respects.
<P>
<PRE>
'a                          =>  a
'#(a b c)                   =>  #(a b c)
'()                         =>  ()
'(+ 1 2)                    =>  (+ 1 2)
'(quote a)                  =>  (quote a)
"a                         =>  (quote a)
</PRE>
<P>
Numerical constants, string constants, character constants, and boolean
constants evaluate "to themselves"; they need not be quoted.
<P>
<PRE>
'"abc"                      =>  "abc"
"abc"                       =>  "abc"
'145932                     =>  145932
145932                      =>  145932
'#t                         =>  #t
#t                          =>  #t
</PRE>
<P>
As noted in section <A HREF="r4rs_5.htm#SEC24">Storage model</A>, it is an error to alter a constant
(i.e. the value of a literal expression) using a mutation procedure like
<CODE>set-car!</CODE> or <CODE>string-set!</CODE>.
<A NAME="IDX63"></A>
<A NAME="IDX64"></A>
<A NAME="IDX62"></A>
<P>
<H3><A NAME="SEC29" HREF="r4rs_toc.htm#SEC29">Procedure calls</A></H3>
<P>
<A NAME="IDX65"></A>
<U>essential syntax:</U> <B>&#60;operator&#62;</B> <I>&#60;operand 1&#62; ...</I><P>
<P>
A procedure call is written by simply enclosing in parentheses
expressions for the procedure to be called and the arguments to be
passed to it.  The operator and operand expressions are evaluated (in an
unspecified order) and the resulting procedure is passed the resulting
arguments.
<A NAME="IDX67"></A>
<A NAME="IDX66"></A>
<P>
<PRE>
(+ 3 4)                     =>  7
((if #f + *) 3 4)           =>  12
</PRE>
<P>
A number of procedures are available as the values of variables in the
initial environment; for example, the addition and multiplication
procedures in the above examples are the values of the variables
<CODE>+</CODE> and <CODE>*</CODE>.
<A NAME="IDX69"></A>
<A NAME="IDX68"></A>
New procedures are created by evaluating lambda expressions (see section
section <A HREF="r4rs_6.htm#SEC30">Lambda expressions</A>).
<P>
Procedure calls are also called <EM>combinations</EM>.
<A NAME="IDX70"></A>
<P>
<EM>Note:</EM>  In contrast to other dialects of Lisp, the order of
evaluation is unspecified, and the operator expression and the operand
expressions are always evaluated with the same evaluation rules.
<P>
<EM>Note:</EM>  Although the order of evaluation is otherwise unspecified, the effect of
any concurrent evaluation of the operator and operand expressions is
constrained to be consistent with some sequential order of evaluation.
The order of evaluation may be chosen differently for each procedure call.
<P>
<EM>Note:</EM>  In many dialects of Lisp, the empty combination,
<CODE>()</CODE>, is a legitimate expression.  In Scheme, combinations must
have at
least one subexpression, so <CODE>()</CODE> is not a syntactically valid
expression.
<P>
<H3><A NAME="SEC30" HREF="r4rs_toc.htm#SEC30">Lambda expressions</A></H3>
<P>
<A NAME="IDX71"></A>
<U>essential syntax:</U> <B>lambda</B> <I>&#60;formals&#62; &#60;body&#62;</I><P>
<P>
<EM>Syntax:</EM>  &#60;Formals&#62; should be a formal arguments list as described below,
and &#60;body&#62; should be a sequence of one or more expressions.
<P>
<EM>Semantics:</EM>  
A lambda expression evaluates to a procedure.  The environment in
effect when the lambda expression was evaluated is remembered as part of the
procedure.  When the procedure is later called with some actual
arguments, the environment in which the lambda expression was evaluated will
be extended by binding the variables in the formal argument list to
fresh locations, the corresponding actual argument values will be stored
in those locations, and the expressions in the body of the lambda expression
will be evaluated sequentially in the extended environment.  The result
of the last expression in the body will be returned as the result of
the procedure call.
<P>
<PRE>
(lambda (x) (+ x x))        =>  <EM>a procedure</EM>
((lambda (x) (+ x x)) 4)    =>  8

(define reverse-subtract
  (lambda (x y) (- y x)))
(reverse-subtract 7 10)     =>  3

(define add4
  (let ((x 4))
    (lambda (y) (+ x y))))
(add4 6)                    =>  10
</PRE>
<P>
&#60;Formals&#62; should have one of the following forms:
<P>
<UL>
<LI><CODE>(&#60;variable 1&#62; ...)</CODE>:
The procedure takes a fixed number of arguments; when the procedure is
called, the arguments will be stored in the bindings of the
corresponding variables.
<P>
<LI>&#60;variable&#62;:
The procedure takes any number of arguments; when the procedure is
called, the sequence of actual arguments is converted into a newly
allocated list, and the list is stored in the binding of the
&#60;variable&#62;.
<P>
<LI><CODE>(&#60;variable 1&#62; ... &#60;variable n-1&#62; . &#60;variable n&#62;)</CODE>:
If a space-delimited period precedes the last variable, then
the value stored in the binding of the last variable will be a
newly allocated
list of the actual arguments left over after all the other actual
arguments have been matched up against the other formal arguments.
</UL>
<P>
It is an error for a &#60;variable&#62; to appear more than once in
&#60;formals&#62;.
<P>
<PRE>
((lambda x x) 3 4 5 6)      =>  (3 4 5 6)
((lambda (x y . z) z)
 3 4 5 6)                   =>  (5 6)
</PRE>
<P>
Each procedure created as the result of evaluating a lambda expression
is tagged with a storage location, in order to make <CODE>eqv?</CODE> and
<CODE>eq?</CODE> work on procedures (see section <A HREF="r4rs_8.htm#SEC47">Equivalence predicates</A>).
<A NAME="IDX73"></A>
<A NAME="IDX72"></A>
<P>
<H3><A NAME="SEC31" HREF="r4rs_toc.htm#SEC31">Conditionals</A></H3>
<P>
<A NAME="IDX74"></A>
<U>essential syntax:</U> <B>if</B> <I>&#60;test&#62; &#60;consequent&#62; &#60;alternate&#62;</I><P>
<A NAME="IDX75"></A>
<U>syntax:</U> <B>if</B> <I>&#60;test&#62; &#60;consequent&#62;</I><P>
<P>
<EM>Syntax:</EM>  &#60;Test&#62;, &#60;consequent&#62;, and &#60;alternate&#62; may be arbitrary
expressions.
<P>
<EM>Semantics:</EM>  An <CODE>if</CODE> expression is evaluated as follows: first,
<A NAME="IDX76"></A>
&#60;test&#62; is evaluated.  If it yields a true value
<A NAME="IDX77"></A>
(see section <A HREF="r4rs_8.htm#SEC46">Booleans</A>), then &#60;consequent&#62; is evaluated
and its value is returned.  Otherwise &#60;alternate&#62; is evaluated and
its value is returned.  If &#60;test&#62; yields a false value and no
&#60;alternate&#62; is specified, then the result of the expression is
unspecified.
<P>
<PRE>
(if (&#62; 3 2) 'yes 'no)       =>  yes
(if (&#62; 2 3) 'yes 'no)       =>  no
(if (&#62; 3 2)
    (- 3 2)
    (+ 3 2))                =>  1
</PRE>
<P>
<H3><A NAME="SEC32" HREF="r4rs_toc.htm#SEC32">Assignments</A></H3>
<P>
<A NAME="IDX78"></A>
<U>essential syntax:</U> <B>set!</B> <I>&#60;variable&#62; &#60;expression&#62;</I><P>
<P>
&#60;Expression&#62; is evaluated, and the resulting value is stored in
the location to which &#60;variable&#62; is bound.  &#60;Variable&#62; must
be bound either in some region
<A NAME="IDX79"></A>
enclosing the <CODE>set!</CODE> expression
<A NAME="IDX80"></A>
or at top level.  The result of the <CODE>set!</CODE> expression is
<A NAME="IDX81"></A>
unspecified.
<P>
<PRE>
(define x 2)
(+ x 1)                     =>  3
(set! x 4)                  =>  <EM>unspecified</EM>
(+ x 1)                     =>  5
</PRE>
<P>
<H2><A NAME="SEC33" HREF="r4rs_toc.htm#SEC33">Derived expression types</A></H2>
<P>
For reference purposes, section <A HREF="r4rs_9.htm#SEC78">derived expression types</A> gives rewrite rules
that will convert constructs described in this section into the
primitive constructs described in the previous section.
<P>
<H3><A NAME="SEC34" HREF="r4rs_toc.htm#SEC34">Conditionals</A></H3>
<P>
<A NAME="IDX82"></A>
<U>essential syntax:</U> <B>cond</B> <I>&#60;clause 1&#62; &#60;clause 2&#62; ...</I><P>
<P>
<EM>Syntax:</EM>  Each &#60;clause&#62; should be of the form
<PRE>
(&#60;test&#62; &#60;expression&#62; ...)
</PRE>
where &#60;test&#62; is any expression.  The last &#60;clause&#62; may be
an "else clause," which has the form
<PRE>
(else &#60;expression 1&#62; &#60;expression 2&#62; ...).
</PRE>
<A NAME="IDX83"></A>
<A NAME="IDX84"></A>
<P>
<EM>Semantics:</EM>  A <CODE>cond</CODE> expression is evaluated by evaluating the &#60;test&#62;
<A NAME="IDX85"></A>
expressions of successive &#60;clause&#62;s in order until one of them
evaluates to a true value
<A NAME="IDX86"></A>
(see section <A HREF="r4rs_8.htm#SEC46">Booleans</A>).  When a &#60;test&#62; evaluates to a
true value, then the remaining &#60;expression&#62;s in its &#60;clause&#62; are
evaluated in order, and the result of the last &#60;expression&#62; in the
&#60;clause&#62; is returned as the result of the entire <CODE>cond</CODE>
<A NAME="IDX87"></A>
expression.  If the selected &#60;clause&#62; contains only the &#60;test&#62;
and no &#60;expression&#62;s, then the value of the &#60;test&#62; is returned
as the result.  If all &#60;test&#62;s evaluate to false values, and there
is no else clause, then the result of the conditional expression is
unspecified; if there is an else clause, then its &#60;expression&#62;s are
evaluated, and the value of the last one is returned.
<P>
<PRE>
(cond ((&#62; 3 2) 'greater)
      ((&#60; 3 2) 'less))      =>  greater

(cond ((&#62; 3 3) 'greater)
      ((&#60; 3 3) 'less)
      (else 'equal))        =>  equal
</PRE>
<P>
Some implementations support an alternative &#60;clause&#62; syntax,
<CODE>(&#60;test&#62; =&#62; &#60;recipient&#62;)</CODE>, where &#60;recipient&#62; is an
expression.  If &#60;test&#62; evaluates to a true value, then
&#60;recipient&#62; is evaluated.  Its value must be a procedure of one
argument; this procedure is then invoked on the value of the
&#60;test&#62;.
<P>
<PRE>
(cond ((assv 'b '((a 1) (b 2))) =&#62; cadr)
      (else <CODE>#f</CODE>))     =>  2
</PRE>
<P>
<A NAME="IDX88"></A>
<U>essential syntax:</U> <B>case</B> <I>&#60;key&#62; &#60;clause 1&#62; &#60;clause 2&#62; ...</I><P>
<P>
<EM>Syntax:</EM>  &#60;Key&#62; may be any expression.  Each &#60;clause&#62; should have
the form
<PRE>
((&#60;datum 1&#62; ...) &#60;expression 1&#62; &#60;expression 2&#62; ...),
</PRE>
where each &#60;datum&#62; is an external representation of some object.
All the &#60;datum&#62;s must be distinct.
The last &#60;clause&#62; may be an "else clause," which has the form
<PRE>
(else &#60;expression 1&#62; &#60;expression 2&#62; ...).
</PRE>
<A NAME="IDX89"></A>
<P>
<EM>Semantics:</EM>  A <CODE>case</CODE> expression is evaluated as follows.  &#60;Key&#62; is
<A NAME="IDX90"></A>
evaluated and its result is compared against each &#60;datum&#62;.  If the
result of evaluating &#60;key&#62; is equivalent (in the sense of
<CODE>eqv?</CODE>; see section <A HREF="r4rs_8.htm#SEC47">Equivalence predicates</A>) to a &#60;datum&#62;, then the
<A NAME="IDX91"></A>
expressions in the corresponding &#60;clause&#62; are evaluated from left
to right and the result of the last expression in the &#60;clause&#62; is
returned as the result of the <CODE>case</CODE> expression.  If the result of
<A NAME="IDX92"></A>
evaluating &#60;key&#62; is different from every &#60;datum&#62;, then if
there is an else clause its expressions are evaluated and the
result of the last is the result of the <CODE>case</CODE> expression;
otherwise
<A NAME="IDX93"></A>
the result of the <CODE>case</CODE> expression is unspecified.
<A NAME="IDX94"></A>
<P>
<PRE>
(case (* 2 3)
  ((2 3 5 7) 'prime)
  ((1 4 6 8 9) 'composite)) =>  composite
(case (car '(c d))
  ((a) 'a)
  ((b) 'b))                 =>  <EM>unspecified</EM>
(case (car '(c d))
  ((a e i o u) 'vowel)
  ((w y) 'semivowel)
  (else 'consonant))        =>  consonant
</PRE>
<P>
<A NAME="IDX95"></A>
<U>essential syntax:</U> <B>and</B> <I>&#60;test 1&#62; ...</I><P>
<P>
The &#60;test&#62; expressions are evaluated from left to right, and the
value of the first expression that evaluates to a false value (see
section <A HREF="r4rs_8.htm#SEC46">Booleans</A>) is returned.  Any remaining expressions
are not evaluated.  If all the expressions evaluate to true values, the
value of the last expression is returned.  If there are no expressions
then <CODE>#t</CODE> is returned.
<P>
<PRE>
(and (= 2 2) (&#62; 2 1))       =>  #t
(and (= 2 2) (&#60; 2 1))       =>  #f
(and 1 2 'c '(f g))         =>  (f g)
(and)                       =>  #t
</PRE>
<P>
<A NAME="IDX96"></A>
<U>essential syntax:</U> <B>or</B> <I>&#60;test 1&#62; ...</I><P>
<P>
The &#60;test&#62; expressions are evaluated from left to right, and the value of the
first expression that evaluates to a true value (see
section <A HREF="r4rs_8.htm#SEC46">Booleans</A>) is returned.  Any remaining expressions
are not evaluated.  If all expressions evaluate to false values, the
value of the last expression is returned.  If there are no
expressions then <CODE>#f</CODE> is returned.
<P>
<PRE>
(or (= 2 2) (&#62; 2 1))        =>  #t
(or (= 2 2) (&#60; 2 1))        =>  #t
(or #f #f #f)               =>  #f
(or (memq 'b '(a b c))
    (/ 3 0))                =>  (b c)
</PRE>
<P>
<H3><A NAME="SEC35" HREF="r4rs_toc.htm#SEC35">Binding constructs</A></H3>
<P>
The three binding constructs <CODE>let</CODE>, <CODE>let*</CODE>, and <CODE>letrec</CODE>
<A NAME="IDX98"></A>
<A NAME="IDX99"></A>
<A NAME="IDX97"></A>
give Scheme a block structure, like Algol 60.  The syntax of the three
constructs is identical, but they differ in the regions
<A NAME="IDX100"></A>
they establish
for their variable bindings.  In a <CODE>let</CODE> expression, the initial
<A NAME="IDX101"></A>
values are computed before any of the variables become bound; in a
<CODE>let*</CODE> expression, the bindings and evaluations are performed
<A NAME="IDX102"></A>
sequentially; while in a <CODE>letrec</CODE> expression, all the bindings are
in
<A NAME="IDX103"></A>
effect while their initial values are being computed, thus allowing
mutually recursive definitions.
<P>
<A NAME="IDX104"></A>
<U>essential syntax:</U> <B>let</B> <I>&#60;bindings&#62; &#60;body&#62;</I><P>
<P>
<EM>Syntax:</EM>  &#60;Bindings&#62; should have the form
<PRE>
((&#60;variable 1&#62; &#60;init 1&#62;) ...),
</PRE>
where each &#60;init&#62; is an expression, and &#60;body&#62; should be a
sequence of one or more expressions.  It is
an error for a &#60;variable&#62; to appear more than once in the list of variables
being bound.
<P>
<EM>Semantics:</EM>  The &#60;init&#62;s are evaluated in the current environment (in some
unspecified order), the &#60;variable&#62;s are bound to fresh locations
holding the results, the &#60;body&#62; is evaluated in the extended
environment, and the value of the last expression of &#60;body&#62; is
returned.  Each binding of a &#60;variable&#62; has &#60;body&#62; as its
region.
<A NAME="IDX105"></A>
<P>
<PRE>
(let ((x 2) (y 3))
  (* x y))                  =>  6

(let ((x 2) (y 3))
  (let ((x 7)
        (z (+ x y)))
    (* z x)))               =>  35
</PRE>
<P>
See also named <CODE>let</CODE>, section <A HREF="r4rs_6.htm#SEC37">Iteration</A>.
<A NAME="IDX106"></A>
<P>
<A NAME="IDX107"></A>
<U>syntax:</U> <B>let*</B> <I>&#60;bindings&#62; &#60;body&#62;</I><P>
<P>
<EM>Syntax:</EM>  &#60;Bindings&#62; should have the form
<PRE>
((&#60;variable 1&#62; &#60;init 1&#62;) ...),
</PRE>
and &#60;body&#62; should be a sequence of
one or more expressions.
<P>
<EM>Semantics:</EM>  <CODE>Let*</CODE> is similar to <CODE>let</CODE>, but the bindings are performed
<A NAME="IDX109"></A>
<A NAME="IDX108"></A>
sequentially from left to right, and the region
<A NAME="IDX110"></A>
of a binding indicated
by <CODE>(&#60;variable&#62; &#60;init&#62;)</CODE> is that part of the <CODE>let*</CODE>
<A NAME="IDX111"></A>
expression to the right of the binding.  Thus the second binding is done
in an environment in which the first binding is visible, and so on.
<P>
<PRE>
(let ((x 2) (y 3))
  (let* ((x 7)
         (z (+ x y)))
    (* z x)))               =>  70
</PRE>
<P>
<A NAME="IDX112"></A>
<U>essential syntax:</U> <B>letrec</B> <I>&#60;bindings&#62; &#60;body&#62;</I><P>
<P>
<EM>Syntax:</EM>  &#60;Bindings&#62; should have the form
<PRE>
((&#60;variable 1&#62; &#60;init 1&#62;) ...),
</PRE>
and &#60;body&#62; should be a sequence of
one or more expressions. It is an error for a &#60;variable&#62; to appear more
than once in the list of variables being bound.
<P>
<EM>Semantics:</EM>  The &#60;variable&#62;s are bound to fresh locations
holding undefined values, the &#60;init&#62;s are evaluated in the resulting
environment (in some unspecified order), each &#60;variable&#62; is assigned
to the result of the corresponding &#60;init&#62;, the &#60;body&#62; is
evaluated in the resulting environment, and the value of the last
expression in &#60;body&#62; is returned.  Each binding of a &#60;variable&#62;
has the entire <CODE>letrec</CODE> expression as its region , making it
<A NAME="IDX113"></A>
possible to define mutually recursive procedures.
<A NAME="IDX114"></A>
<P>
<PRE>

(letrec ((even?
          (lambda (n)
            (if (zero? n)
                #t
                (odd? (- n 1)))))
         (odd?
          (lambda (n)
            (if (zero? n)
                #f
                (even? (- n 1))))))
  (even? 88))
                            =>  #t
</PRE>
<P>
One restriction on <CODE>letrec</CODE> is very important: it must be possible
<A NAME="IDX115"></A>
to evaluate each &#60;init&#62; without assigning or referring to the value of any
&#60;variable&#62;.  If this restriction is violated, then it is an error.  The
restriction is necessary because Scheme passes arguments by value rather than by
name.  In the most common uses of <CODE>letrec</CODE>, all the &#60;init&#62;s are
<A NAME="IDX116"></A>
lambda expressions and the restriction is satisfied automatically.
<P>
<H3><A NAME="SEC36" HREF="r4rs_toc.htm#SEC36">Sequencing</A></H3>
<P>
<A NAME="IDX117"></A>
<U>essential syntax:</U> <B>begin</B> <I>&#60;expression 1&#62; &#60;expression 2&#62; ...</I><P>
<P>
The &#60;expression&#62;s are evaluated sequentially from left to right,
and the value of the last &#60;expression&#62; is returned.  This
expression type is used to sequence side effects such as input and
output.
<P>
<PRE>
(define x 0)

(begin (set! x 5)
       (+ x 1))             =>  6

(begin (display "4 plus 1 equals ")
       (display (+ 4 1)))   =>  <EM>unspecified</EM>
        <EM>and prints</EM>  4 plus 1 equals 5
</PRE>
<P>
<EM>Note:</EM>  <A HREF="r4rs_13.htm#BIB2">[SICP]</A> uses the keyword <CODE>sequence</CODE> instead of <CODE>begin</CODE>.
<A NAME="IDX119"></A>
<A NAME="IDX118"></A>
<P>
<H3><A NAME="SEC37" HREF="r4rs_toc.htm#SEC37">Iteration</A></H3>
<P>
<A NAME="IDX120"></A>
<U>syntax:</U> <B>do</B> <I>&#60;bindings&#62; &#60;clause&#62; &#60;body&#62;</I><P>
<P>
<EM>Syntax:</EM>  &#60;Bindings&#62; should have the form
<PRE>
((&#60;variable 1&#62; &#60;init 1&#62; &#60;step 1&#62;) ...),
</PRE>
&#60;clause&#62; should be of the form
<PRE>
(&#60;test&#62; &#60;expression&#62; ...),
</PRE>
and &#60;body&#62; should be a sequence of one or more expressions.
<P>
<CODE>Do</CODE> is an iteration construct.  It specifies a set of variables to
be bound, how they are to be initialized at the start, and how they are
to be updated on each iteration.  When a termination condition is met,
the loop exits with a specified result value.
<P>
<CODE>Do</CODE> expressions are evaluated as follows:
<A NAME="IDX121"></A>
The &#60;init&#62; expressions are evaluated (in some unspecified order),
the &#60;variable&#62;s are bound to fresh locations, the results of the
&#60;init&#62; expressions are stored in the bindings of the
&#60;variable&#62;s, and then the iteration phase begins.
<P>
Each iteration begins by evaluating &#60;test&#62;; if the result is
false (see section <A HREF="r4rs_8.htm#SEC46">Booleans</A>), then the &#60;command&#62;
expressions are evaluated in order for effect, the &#60;step&#62;
expressions are evaluated in some unspecified order, the
&#60;variable&#62;s are bound to fresh locations, the results of the
&#60;step&#62;s are stored in the bindings of the
&#60;variable&#62;s, and the next iteration begins.
<P>
If &#60;test&#62; evaluates to a true value, then the
&#60;expression&#62;s are evaluated from left to right and the value of
the last &#60;expression&#62; is returned as the value of the <CODE>do</CODE>
<A NAME="IDX122"></A>
expression.  If no &#60;expression&#62;s are present, then the value of
the <CODE>do</CODE> expression is unspecified.
<A NAME="IDX123"></A>
<P>
The region
<A NAME="IDX124"></A>
of the binding of a &#60;variable&#62; consists of the entire <CODE>do</CODE>
<A NAME="IDX125"></A>
expression except for the &#60;init&#62;s.  It is an error for a
&#60;variable&#62; to appear more than once in the list of <CODE>do</CODE>
<A NAME="IDX126"></A>
variables.
<P>
A &#60;step&#62; may be omitted, in which case the effect is the
same as if <CODE>(&#60;variable&#62; &#60;init&#62; &#60;variable&#62;)</CODE> had
been written instead of <CODE>(&#60;variable&#62; &#60;init&#62;)</CODE>.
<P>
<PRE>
(do ((vec (make-vector 5))
     (i 0 (+ i 1)))
    ((= i 5) vec)
  (vector-set! vec i i))    =>  #(0 1 2 3 4)

(let ((x '(1 3 5 7 9)))
  (do ((x x (cdr x))
       (sum 0 (+ sum (car x))))
      ((null? x) sum)))     =>  25
</PRE>
<P>
<A NAME="IDX127"></A>
<U>syntax:</U> <B>let</B> <I>&#60;variable&#62; &#60;bindings&#62; &#60;body&#62;</I><P>
<P>
Some implementations of Scheme permit a variant on the syntax of
<CODE>let</CODE> called "named <CODE>let</CODE>" which provides a more general
<A NAME="IDX129"></A>
<A NAME="IDX128"></A>
looping construct than <CODE>do</CODE>, and may also be used to express
<A NAME="IDX130"></A>
recursions.
<P>
Named <CODE>let</CODE> has the same syntax and semantics as ordinary
<A NAME="IDX131"></A>
<CODE>let</CODE> except that &#60;variable&#62; is bound within &#60;body&#62; to a
procedure whose formal arguments are the bound variables and whose body
is &#60;body&#62;.  Thus the execution of &#60;body&#62; may be repeated by
invoking the procedure named by &#60;variable&#62;.
<P>
<PRE>
(let loop ((numbers '(3 -2 1 6 -5))
           (nonneg '())
           (neg '()))
  (cond ((null? numbers) (list nonneg neg))
        ((&#62;= (car numbers) 0)
         (loop (cdr numbers)
               (cons (car numbers) nonneg)
               neg))
        ((&#60; (car numbers) 0)
         (loop (cdr numbers)
               nonneg
               (cons (car numbers) neg)))))
                            =>  ((6 1 3) (-5 -2))
</PRE>
<P>
<H3><A NAME="SEC38" HREF="r4rs_toc.htm#SEC38">Delayed evaluation</A></H3>
<P>
<A NAME="IDX132"></A>
<U>syntax:</U> <B>delay</B> <I>&#60;expression&#62;</I><P>
<P>
The <CODE>delay</CODE> construct is used together with the
procedure <CODE>force</CODE> to
<A NAME="IDX133"></A>
implement <DFN>lazy evaluation</DFN> or <DFN>call by need</DFN>.
<CODE>(delay &#60;expression&#62;)</CODE> returns an object called a
<DFN>promise</DFN> which at some point in the future may be asked (by
the <CODE>force</CODE> procedure)
to evaluate &#60;expression&#62; and deliver the resulting value.
<P>
See the description of <CODE>force</CODE> (section <A HREF="r4rs_8.htm#SEC60">Control features</A>) for a
<A NAME="IDX134"></A>
more complete description of <CODE>delay</CODE>.
<P>
<H3><A NAME="SEC39" HREF="r4rs_toc.htm#SEC39">Quasiquotation</A></H3>
<P>
<A NAME="IDX135"></A>
<U>essential syntax:</U> <B>quasiquote</B> <I>&#60;template&#62;</I><P>
<A NAME="IDX136"></A>
<U>essential syntax:</U> <B>`</B> <I>&#60;template&#62;</I><P>
<P>
"Backquote" or "quasiquote"
<A NAME="IDX137"></A>
expressions are useful
for constructing a list or vector structure when most but not all of the
desired structure is known in advance.  If no
commas
<A NAME="IDX138"></A>
appear within the &#60;template&#62;, the result of evaluating
<CODE>`&#60;template&#62;</CODE> is equivalent to the result of evaluating
<CODE>'&#60;template&#62;</CODE>.  If a comma
<A NAME="IDX139"></A>
appears within the
&#60;template&#62;, however, the expression following the comma is
evaluated ("unquoted") and its result is inserted into the structure
instead of the comma and the expression.  If a comma appears followed
immediately by an at-sign (@),
<A NAME="IDX140"></A>
then the following
expression must evaluate to a list; the opening and closing parentheses
of the list are then "stripped away" and the elements of the list are
inserted in place of the comma at-sign expression sequence.
<A NAME="IDX141"></A>
<P>
<PRE>
`(list ,(+ 1 2) 4)          =>  (list 3 4)
(let ((name 'a)) `(list ,name ',name))
                            =>  (list a (quote a))
`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)
                            =>  (a 3 4 5 6 b)
`((<CODE>foo</CODE> ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))
<A NAME="IDX142"></A>                            =>  ((foo 7) . cons)
`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8)
                            =>  #(10 5 2 4 3 8)
</PRE>
<P>
Quasiquote forms may be nested.  Substitutions are made only for
unquoted components appearing at the same nesting level
as the outermost backquote.  The nesting level increases by one inside
each successive quasiquotation, and decreases by one inside each
unquotation.
<P>
<PRE>
`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
                            =>  (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
(let ((name1 'x)
      (name2 'y))
  `(a `(b ,,name1 ,',name2 d) e))
                            =>  (a `(b ,x ,'y d) e)
</PRE>
<A NAME="IDX143"></A>
<P>
The notations <CODE>`&#60;template&#62;</CODE> and
<CODE>(quasiquote &#60;template&#62;)</CODE> are identical in all respects.
<CODE>,&#60;expression&#62;</CODE> is identical to <CODE>(unquote
&#60;expression&#62;)</CODE>, and <CODE>,&#60;expression&#62;</CODE> is identical to
<CODE>(unquote-splicing &#60;expression&#62;)</CODE>.  The external syntax
generated by <CODE>write</CODE> for two-element lists whose
<A NAME="IDX144"></A>
car is one of these symbols may vary between implementations.
<P>
<PRE>
(quasiquote (list (unquote (+ 1 2)) 4))
                            =>  (list 3 4)
'(quasiquote (list (unquote (+ 1 2)) 4))
                            =>  `(list ,(+ 1 2) 4)
     <EM>i.e.,</EM> (quasiquote (list (unquote (+ 1 2)) 4))
</PRE>
<P>
Unpredictable behavior can result if any of the symbols
<CODE>quasiquote</CODE>, <CODE>unquote</CODE>, or <CODE>unquote-splicing</CODE>
<A NAME="IDX146"></A>
<A NAME="IDX145"></A>
appear in
<A NAME="IDX147"></A>
positions within a &#60;template&#62; otherwise than as described above.
<P>
<P>Go to the <A HREF="r4rs_5.htm">previous</A>, <A HREF="r4rs_7.htm">next</A> section.<P>
&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>